package com.shop.product.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shop.product.service.ProductService;
import com.shop.product.vo.ProductVO;

/**
 * request = /rest/CATEGORY/
 * @author geryon
 *
 */
@Controller
@RequestMapping("/PRODUCT")
public class ProductController {

	private final static int LIST_ROW_CNT = 20; // 게시물 리스트 갯수
	
	@Autowired
	private ProductService service;

	/**
	 * 상품 메인
	 * 리스트 화면을 호출한다
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/")
	public String requestScreen(Model model) {
		return "product/product_list";
	}

	/**
	 * 상품 리스트
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/list")
	public Model listProduct(Model model,
							@RequestParam ("page_no") int page_no,
							@RequestParam ("src_type") String src_type,
							@RequestParam ("src_keyword") String src_keyword) {
		
		int tot_cnt = 0; // 전체건수
		int tot_page_cnt = 0; // 총페이지수
		
		//총 아이템 구하기
		tot_cnt = service.getProductCount(src_type, src_keyword);

		// 총페이지
		tot_page_cnt = tot_cnt / LIST_ROW_CNT + (((tot_cnt % LIST_ROW_CNT) == 0) ? 0:1);
		
		// 총 사용량 리스트 조회
		List<ProductVO.ProductItem> list = service.listProduct(page_no, LIST_ROW_CNT, src_type, src_keyword);
		
		model.addAttribute("TOTAL_COUNT", tot_cnt);
		model.addAttribute("TOTAL_PAGE_COUNT", tot_page_cnt);
		model.addAttribute("MODEL", list);
		
		return model;
	}
	
	/**
	 * 상품 등록
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value="/add")
	public Model addProduct(Model model,
							@RequestBody ProductVO.ProductItem productItem) {
		
		service.addProduct(productItem);
		
		return model;
	}

	/**
	 * 상품 상세조회
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value="/{member_seq}")
	public Model getProduct(Model model,
							@PathVariable ("member_seq") String member_seq) {
		
		return model;
	}

	/**
	 * 상품 수정
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value="/{member_seq}")
	public Model updateProduct(Model model,
							@PathVariable ("member_seq") String member_seq,
							@RequestBody ProductVO.ProductItem productItem) {
		
		return model;
	}

	/**
	 * 상 삭제
	 * @param model
	 * @param member
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value="/{member_seq}")
	public Model deleteProduct(Model model,
							@PathVariable ("member_seq") String member_seq) {
		
		return model;
	}

}
